﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using HawaiRestaurant.Models.Entities;
using Microsoft.Office.Interop.Excel;

namespace HawaiRestaurant.Models.Helper
{
    public class ExcelHelper
    {
        public void ExportOrderReport(IEnumerable<OrderReportEntities> entities, string filePath)
        {
            object misValue = Missing.Value;

            var xlApp = new Application();
            Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
            var xlWorkSheet = (Worksheet) xlWorkBook.Worksheets.Item[1];

            xlWorkSheet.Cells[1, 1] = "OrderId";
            xlWorkSheet.Cells[1, 2] = "OrderDate";
            xlWorkSheet.Cells[1, 3] = "CustomerId";
            xlWorkSheet.Cells[1, 4] = "CustomerName";
            xlWorkSheet.Cells[1, 5] = "OrderValue";

            OrderReportEntities[] tempArray = entities as OrderReportEntities[] ?? entities.ToArray();
            for (int i = 0; i <= tempArray.Count() - 1; i++)
            {
                xlWorkSheet.Cells[i + 2, 1] = tempArray[i].OrderId.ToString(CultureInfo.CurrentCulture);
                xlWorkSheet.Cells[i + 2, 2] = tempArray[i].OrderDate.ToShortDateString();
                xlWorkSheet.Cells[i + 2, 3] = tempArray[i].CustomerId.ToString(CultureInfo.CurrentCulture);
                xlWorkSheet.Cells[i + 2, 4] = tempArray[i].CustomerName;
                xlWorkSheet.Cells[i + 2, 5] = tempArray[i].OrderValue.ToString(CultureInfo.CurrentCulture);
            }

            xlWorkBook.SaveAs(filePath, XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue,
                              misValue, XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        public void ExportOrderSummaryReport(IEnumerable<OrderSummaryEntities> entities, string filePath)
        {
            object misValue = Missing.Value;

            var xlApp = new Application();
            var xlWorkBook = xlApp.Workbooks.Add(misValue);
            var xlWorkSheet = (Worksheet) xlWorkBook.Worksheets.Item[1];

            //add data 
            var arrayData = entities.ToArray();
            xlWorkSheet.Cells[1, 1] = "";
            xlWorkSheet.Cells[1, 2] = "Order Summary Report";

            for (int i = 0; i < arrayData.Count(); i++)
            {
                xlWorkSheet.Cells[i + 2, 1] = arrayData[i].ContractTotalAmount;
                xlWorkSheet.Cells[i + 2, 2] = arrayData[i].NumberOfOrder;
            }

            var xlCharts = (ChartObjects) xlWorkSheet.ChartObjects(Type.Missing);
            var myChart = xlCharts.Add(80, 20, 500, 250);
            var chartPage = myChart.Chart;

            var chartRange = xlWorkSheet.Range["A1", "B5"];
            chartPage.SetSourceData(chartRange, misValue);
            chartPage.ChartType = XlChartType.xlColumnClustered;

            xlWorkBook.SaveAs(filePath, XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
                              XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        private void releaseObject(object obj)
        {
            Marshal.ReleaseComObject(obj);
        }
    }
}